package de.baumann.browser.activitys.center

import android.content.Context
import android.content.Intent
import de.baumann.browser.R
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.iview.ICreatePoolView
import de.baumann.browser.present.CreatePoolPresenter
import de.baumann.browser.units.TimerUnit
import de.baumann.browser.views.dialog.Callback
import de.baumann.browser.views.dialog.CreatePoolSuccessDialog
import de.baumann.browser.views.dialog.RuleDialog
import de.baumann.browser.views.dialog.VaptchaDialog
import kotlinx.android.synthetic.main.activity_create_pool.*
import kotlinx.android.synthetic.main.include_tool_bar.*

/**
 *@author:
 *@date:2019/7/1 18:26
 *@discription: 成为矿池
 */
class CreatePoolActivity : BaseOdinActivity<ICreatePoolView, CreatePoolPresenter>(), ICreatePoolView {

    private lateinit var timerUnit: TimerUnit

    companion object {
        fun startCreatePoolActivity(context: Context) {
            context.startActivity(Intent(context, CreatePoolActivity::class.java))
        }
    }

    override fun getLayout(): Int {
        return R.layout.activity_create_pool
    }

    override fun initView() {
        initToolbar(commToolBar, null)
        createPoolRule.setOnClickListener {
            RuleDialog(mContext)
                    .setRuleTitle("申请规则")
                    .setRuleContent(getString(R.string.createPoolRule))
                    .show()
        }

        btnCreatePool.setOnClickListener {
            presenter.createPool()
        }
        tvCreatePoolGetCode.setOnClickListener {
            VaptchaDialog(mContext,object :Callback{
                override fun callback(token: String) {
                    runOnUiThread {
                        presenter.getSmsCode(token)
                        timerUnit.startTime()
                    }
                }
            }).show()
        }
    }

    override fun createPresenter(): CreatePoolPresenter {
        return CreatePoolPresenter()
    }

    override fun createView(): ICreatePoolView {
        return this
    }

    override fun initData() {
        timerUnit = TimerUnit(tvCreatePoolGetCode)
    }

    override fun onError(errorMsg: String?) {
        super.onError(errorMsg)
        timerUnit.endtTime()
    }

    override fun onError(errorMsgId: Int) {
        super.onError(errorMsgId)
        timerUnit.endtTime()
    }

    override fun onDestroy() {
        super.onDestroy()
        timerUnit.endtTime()
    }

    override fun createPoolSuccess() {
        val dialog = CreatePoolSuccessDialog(mContext)
        dialog.setOnDismissListener {
            finish()
        }
        dialog.show()
    }

    override val telNum: String
        get() = edCreatePoolPhone.text.trim().toString()
    override val smsCode: String
        get() = edCreatePoolCode.text.trim().toString()

}
